home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / lcppb.zip / LCPP04.ZIP / COLUMN.CPP < prev    next >
C/C++ Source or Header  |  1991-07-03  |  1KB  |  51 lines

  1. // column.cpp -- Extract a column from input lines
  2.  
  3. //#include <stream.hpp>
  4. #include <iostream.h>
  5. #include <stdlib.h>
  6. #include <stdio.h>
  7.  
  8. #define NEWLINE '\n'
  9.  
  10. main(int argc, char *argv[])
  11. {
  12.   char c;              // I/O character
  13.   int cstart, cend;    // Start and end column numbers
  14.   int cpos = 0;        // Column position on current line
  15.  
  16. /* -- Display instructions if 1 or 0 arguments entered */
  17.  
  18.   if (argc < 3) {
  19.     cerr << "COLUMN  v1.00  (c) 1991 by Tom Swan\n";
  20.     cerr << "To use: Enter column number and width\n";
  21.     cerr << "Example: dir | column 1 12\n";
  22.     exit(1);
  23.   }
  24.  
  25. /* -- Calculate starting and ending columns */
  26.  
  27.   cstart = atoi(argv[1]);
  28.   cend = cstart + atoi(argv[2]) - 1;
  29.   if (cstart < 1) cstart = 1;
  30.   if (cend < cstart) cend = cstart;
  31.  
  32. /* -- Copy selected column from input lines to output */
  33.  
  34.   while ((c = getchar()) != EOF) {
  35.     cpos++;
  36.     if (c == NEWLINE) {
  37.       cout << NEWLINE;
  38.       cpos = 0;
  39.     } else if ((cstart <= cpos) && (cpos <= cend))
  40.         putchar(c);
  41.   }
  42. }
  43.  
  44.  
  45. // Copyright (c) 1990 by Tom Swan. All rights reserved
  46. // Revision 1.00    Date: 08/02/1990   Time: 09:24 pm
  47.  
  48. // Revision 1.01    Date: 07/03/1991   Time: 04:00 pm
  49. // Converted for Borland C++ 2.0
  50.  
  51.